<?php

/**
 * winbinder 패키지 
 * 
 * 
 * @package php.ui.winbinder
 */


/**
 * WBTimer 
 * 
 * 타이머 구현 클래스 
 *
 * @package php.ui.winbinder
 */

class WBTimer extends WBObject { 
	
	private $_instance = null;


	/**
	 * 생성자 
	 * 
	 * @param array $options
	 */
	public function __construct($options = array()) { 

		parent::__construct($options);

		$this->createTimer();
	}

	public function __destruct() {
		$this->destroyTimer();
				
		parent::__destruct();
		
		unset($this->_instance);
	}
	
	/**
	 * 타이머 instance 생성 
	 * 
	 */
	public function createTimer() { 
		$this->setInstance(wb_create_timer(wbInstance(wbParent($this['parent'])), $this->getOption('id'), $this->getOption('interval')));
	}

	/**
	 * 타이머 instance 설정
	 * 
	 * @param int $instance
	 */
	public function setInstance($instance) { 
		$this->_instance = $instance;
	}

	/**
	 * 타이머 instance 얻어오기 
	 * 
	 * @return int
	 */
	public function getInstance() { 
		return $this->_instance;
	}

	/**
	 * 타이머 삭제 
	 * 
	 * @return boolean
	 */
	public function destroyTimer() {
		if ($this->getInstance()) {  
			$instance = wbInstance(wbparent($this));
			if ($instance) { 
				return wb_destroy_timer($instance, $this->getOption('id'));
			}
		}
	}

	/**
	 * 타이머 이벤트 설정
	 * 
	 * click
	 * 
	 * @see lib/php/ui/winbinder/WBObject#initEvent()
	 */
	public function initEvent() { 
		parent::initEvent();

		$this->setEvent('click', 'onClick');
	}

	/**
	 * 이벤트 실행 구현 
	 * 
	 * @see lib/php/ui/winbinder/WBObject#fire($eo)
	 */
	public function fire($eo) { 
		$this->fireEvent('click', $eo);
	}
	
	/**
	 * click 이벤트 자동 실행 
	 * 
	 * @param WBEvent $eo
	 */
	public function onClick($eo) {
		if (isset($this->callback)) { 
			call_user_func_array($this->getOption('callback'), array($eo));
		}
	}
	

}

?>